import baseURL from "@/api/baseURL"
export const mymixin = {
  data () {
    return {
      nowpage: 1,
      loading: true,
      // 输入框是否可修改true为可修改
      canInput: false,
      // 添加删除修改状态---主要通过此状态来判断调用方法
      nowChanceStau: '',
      // 操作按钮的逻辑及展示数据
      addActive: true,
      delActive: false,
      saveActive: false,
      cancelActive: false,
      editActive: false,
      dialogFormVisible: false,
      // 表格数据
      tableData: [],
      // 表单数据
      formData: {},
      // treeData: [],
      image: null,
      // 当前页
      currentPage: 1,
      // 每页的数据条数
      pageSize: 10,
      // 总条数
      totalSize: 0,
      // 总页数
      totalPages: 0,
      allPointList: [],
      rightTableData: [],
      unitTypeId: '',
      unitsList: [],
      deleteFlag: true,
      tagsList: [],
      addformData: {},
      rules: {},
      postName: "",
      // 图片上传
      basicUrl: `${baseURL.webServiceUrl}om/collectionPicture/`,
      // 重置
      reseUrl: `${baseURL.webServiceUrl}om/collectionPicture/`,
      pictureId: '',
      token: "",
      InImgUrl: '',
      funcAppAuthActive: false,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        },
        {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        },
        {
          text: '最近一年',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
            picker.$emit('pick', [start, end]);
          }
        }
        ]
      },
      // 地址选择
      selectedOptions: [],
      address: "",
      // 弹出框添加修改
      titleText: null,
      currentRow: null,
      imgList: [],
      // 搜索框的显示与隐藏
      activeName: ''
    }
  },
  created () {
    this.token = sessionStorage.getItem("token");
  },
  watch: {
    nowChanceStau (newval) {
      // 当状态为删除的时候触发删除方法---另外的删除修改添加，通过点击保存，调用方法来实现
      if (newval === 'del') {
        this.delmessage('formData')
      }
    }
  },
  computed: {
    headers () {
      return {
        token: sessionStorage.getItem("token"),
        homeBaseId: sessionStorage.getItem("homeBaseId"),
        homeBaseType: sessionStorage.getItem("homeBaseType")
      }
    }
  },
  methods: {
    selectable (row) {
      return !(row.index)
    },
    // select搜索框
    changeSelect () {
      if (this.activeName === '') {
        this.activeName = '1'
      } else if (this.activeName === '1') {
        this.activeName = ''
      }
    },
    // 填充表格空白行
    addblankmet () {
      let havline = this.tableData.length % this.pageSize
      if (this.tableData.length < this.pageSize) {
        if (havline < this.pageSize) {
          const remain = this.pageSize - havline;
          for (var i = 0; i < remain; i++) {
            this.tableData.push({
              'index': i + this.tableData.length - 1, selectable: true
            });
          }
        }
      }
    },
    handleCurrentChange (val) {
      this.currentRow = val;
    },
    search () {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize (pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage (currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    // 添加
    add () {
      this.canInput = true;
      this.nowChanceStau = 'add'
      this.formData = {}
      this.image = null;
      this.tagsList = [];
      this.unitTypeId = ''
      this.unitsList = []
      this.basicUrl = this.reseUrl;
      this.postName = ""
      this.InImgUrl = ''
      this.titleText = "添加"
      this.dialogFormVisible = true;
      this.addActive = false;
      this.delActive = false;
      this.saveActive = true;
      this.cancelActive = true;
      this.editActive = false;
    },
    // 修改
    edit () {
      this.nowChanceStau = 'edit'
      this.basicUrl = this.reseUrl;
      this.titleText = "修改"
      this.dialogFormVisible = true;
      this.canInput = true;
      this.addActive = false;
      this.delActive = false;
      this.saveActive = true;
      this.cancelActive = true;
      this.editActive = false
    },
    // 删除
    del () {
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      })
        .then(() => {
          this.nowChanceStau = 'del'
          this.delmessage()
          this.resetActionBar()
        })
        .catch(() => {
          this.resetActionBar()
          this.$message({
            type: "info",
            message: "已取消删除"
          });
          if (this.tableData.length > 0) {
            this.$refs.tableData.clearSelection();
          } else if (this.treeData.length > 0) {
            this.$refs.tree.setCheckedKeys([]);
          }
        });
      this.formData = {}
      this.postName = ""
      this.nowChanceStau = "";
      this.canInput = true;
      this.addActive = true;
      this.delActive = false
      this.saveActive = false;
      this.editActive = false
      this.cancelActive = false;
    },
    // 取消
    cancel (formData) {
      this.deleteFlag = true;
      this.nowChanceStau = '';
      this.formData = {};
      this.pictureId = '';
      this.basicUrl = "";
      this.rightTableData = [];
      this.tagsList = [];
      this.selectedOptions = [];
      this.unitTypeId = ''
      this.unitsList = []
      this.postName = ""
      this.address = "";
      this.dialogFormVisible = false;
      this.addformData = {}
      this.titleText = "查看"
      this.canInput = false;
      this.addActive = true;
      this.delActive = false;
      this.saveActive = false;
      this.cancelActive = false;
      this.editActive = false;
      // if (formData) {
      //   this.$refs[formData].resetFields();
      // }
    },
    // 初始化操作栏状态
    resetActionBar () {
      this.nowChanceStau = ''
      this.formData = {}
      this.rightTableData = []
      this.image = null;
      this.pictureId = ''
      this.unitTypeId = ''
      this.unitsList = []
      this.InImgUrl = ''
      this.basicUrl = this.reseUrl;
      this.postName = ""
      this.canInput = false;
      this.dialogFormVisible = false;
      this.funcAppAuthActive = false
      this.addActive = true;
      this.delActive = false;
      this.imgList = [];
      this.$refs.tableData.setCurrentRow()
      this.$refs.tableData.clearSelection()
      this.saveActive = false;
      this.cancelActive = false;
      this.editActive = false
      this.funcAuthActive = false
      this.funcAppAuthActive = false
    },
    closeDialog () {
      if (this.showAddEditDialog === true || this.showReasonDialog === true) {
        this.search()
      }
      this.showAddEditDialog = false
      this.showPictureDialog = false
      this.showVideoDialog = false
      this.showKeyDialog = false
      this.showReasonDialog = false
      this.nowChanceStau = '';
      this.titleText = ''
      this.editObject = {}
      this.addActive = true;
      this.delActive = false;
      this.$refs.tableData.setCurrentRow()
      this.$refs.tableData.clearSelection()
    },
    // 获取当前时间以及日期
    getNowTime () {
      let now = new Date();
      let year = now.getFullYear(); // 获取年
      let month = now.getMonth();// 获取月
      let date = now.getDate();// 获取日
      let hours = now.getHours();// 获取小时
      let minutes = now.getMinutes();// 获取分钟
      let seconds = now.getSeconds();// 获取秒
      month = month + 1;
      month = month.toString().padStart(2, "0");
      date = date.toString().padStart(2, "0");
      return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;
    },
    dateToFormattwo (originDate) {
      const dt = new Date(originDate);
      const y = dt.getFullYear();
      const m = (dt.getMonth() + 1 + "").padStart(2, "0");
      const d = (dt.getDate() + "").padStart(2, "0");
      return `${y}-${m}-${d}`;
    },
    processName (str) {
      if (null != str && str != undefined) {
        let star = '' //存放名字中间的*
        //名字是两位的就取姓名首位+*
        if (str.length <= 2) {
          return str.substring(0, 1) + "*";
        } else {
          for (var i = 0; i < str.length - 2; i++) {
            star = star + '*'
          }
          return str.substring(0, 1) + star + str.substring(str.length - 1, str.length);
        }
      }
    },
  }
}
